<?php
namespace app\shop\controller\sys_admin;
use app\AdminController;
use app\member\model\UsersModel;
use app\shop\model\CuisineModel;
use app\shop\model\CuisineOrderModel;
use app\shop\model\StoreModel;
use app\shop\model\StoreStaffModel;
use think\Db;

/**
 * 商品品牌
 * Class Index
 * @package app\store\controller
 */
class Cuisine extends AdminController
{
    public $is_supplyer = false;//是否后台供应商管理
    public $supplyer_id = 0;//当前操作的供应商ID
	//*------------------------------------------------------ */
	//-- 初始化
	/*------------------------------------------------------ */
   public function initialize()
   {
   		parent::initialize();
		$this->Model = new CuisineModel();
        $this->assign('is_supplyer', $this->is_supplyer);
    }
	//*------------------------------------------------------ */
	//-- 首页
	/*------------------------------------------------------ */
    public function index()
    {
        $this->getList(true);
        return $this->fetch();
    }

    /*------------------------------------------------------ */
    //-- 获取列表
    //-- $runData boolean 是否返回模板
    /*------------------------------------------------------ */
    public function getList($runData = false)
    {
        $where = [];
        $search['keyword'] = input('keyword', '', 'trim');
        if (empty($search['keyword']) == false) $where[] = ['store_name', 'like', '%' . $search['keyword'] . '%'];
        $data = $this->getPageList($this->Model, $where);
        $userModel = new UsersModel();
        $storeModel = new StoreModel();
        foreach ($data['list'] as &$value){

        }
        $this->assign("data", $data);
        $this->assign("search", $search);
        if ($runData == false) {
            $data['content'] = $this->fetch('list')->getContent();
            unset($data['list']);
            return $this->success('', '', $data);
        }
        return true;
    }
    /*------------------------------------------------------ */
    //-- 信息页调用
    //-- $data array 自动读取对应的数据
    /*------------------------------------------------------ */
    public function asInfo($data){

        return $data;
    }

	/*------------------------------------------------------ */
	//-- 添加前处理
	/*------------------------------------------------------ */
    public function beforeAdd($data) {
		return $data;
	}

	/*------------------------------------------------------ */
	//-- 修改前处理
	/*------------------------------------------------------ */
    public function beforeEdit($data){
		return $data;
	}
	/*------------------------------------------------------ */
	//-- 删除分类
	/*------------------------------------------------------ */
	public function del(){
		$map['id'] = input('id',0,'intval');
		if ($map['id']<1) return $this->error('传递参数失败！');
		$this->Model->where($map)->update(['is_del' => 1]);
		return $this->success('操作成功.');
	}

    public function ajaxEdit(){
        $id = input('id',0,'intval');
        $field = input('field','','trim');
        if ($id=='' || $field=='') return $this->error('缺少必要传参.');
        $row = $this->Model->find($id);
        if ($field == 'status'){
            $upArr['status'] = $row['status'] == 1 ? 0: 1;
        }
        $this->Model->save($upArr,['id'=>$id]);
        return $this->success();
    }

    //*------------------------------------------------------ */
    //-- 首页
    /*------------------------------------------------------ */
    public function order()
    {
        $this->getOrderList(true);
        return $this->fetch();
    }

    /*------------------------------------------------------ */
    //-- 获取列表
    //-- $runData boolean 是否返回模板
    /*------------------------------------------------------ */
    public function getOrderList($runData = false)
    {
        $where = [];
        $model = new CuisineOrderModel();
        $search['keyword'] = input('keyword', '', 'trim');
        if (empty($search['keyword']) == false) $where[] = ['store_name', 'like', '%' . $search['keyword'] . '%'];
        $data = $this->getPageList($model, $where);
        $userModel = new UsersModel();
        $storeModel = new StoreModel();
        foreach ($data['list'] as &$value){
            $value['cuisine_name'] = Db::name('shop_cuisine')->where(['id' => $value['cuisine_id']])->value('name');
            $value['pay_name'] = '餐券';
            if($value['pay_code'] == 'weiixn'){
                $value['pay_name'] = '微信支付';
            }elseif($value['pay_code'] == 'balance'){
                $value['pay_name'] = '积分支付';
            }
            $value['add_time'] = date('Y-m-d H:i',$value['add_time'] );
        }
        $this->assign("data", $data);
        $this->assign("search", $search);
        if ($runData == false) {
            $data['content'] = $this->fetch('list')->getContent();
            unset($data['list']);
            return $this->success('', '', $data);
        }
        return true;
    }

    public function complete(){
        $id = input('id',0,'intval');
        if ($id=='') return $this->error('缺少必要传参.');
        $model = new CuisineOrderModel();
        $upArr['status'] = 1;
        $model->save($upArr,['id'=>$id]);
        return $this->success('已完成');
    }
}
